﻿using AutoMapper;
using ManagementApi.Models.Corp;
using Modules.Attachment;
using Modules.Project;
using Modules.System.Attachment;
using PmSoft.ApplicationContext;
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;

namespace ManagementApi.Models.Project;

public class ProjectCompletForm
{
    /// <summary>
    /// 项目ID
    ///</summary>
    [Display(Name = "项目ID")]
    [Required(ErrorMessage = "{0}不能为空")]
    public int ProjectId { get; set; }

    /// <summary>
    /// 施工许可证编号
    ///</summary>
    [Display(Name = "施工许可证编号")]
    [Required(ErrorMessage = "{0}不能为空")]
    public string BuilderLicences { get; set; } = string.Empty;

    /// <summary>
    /// 施工许可证发证日期
    ///</summary>
    [Display(Name = "施工许可证发证日期")]
    [Required(ErrorMessage = "{0}不能为空")]
    public DateTime? BuilderLicencesGrantDate { get; set; }

    /// <summary>
    /// 施工许可证有效期限
    ///</summary>
    [Display(Name = "施工许可证有效期限")]
    [Required(ErrorMessage = "{0}不能为空")]
    public DateTime? BuilderLicencesTermDate { get; set; }

    /// <summary>
    /// 是否有工程款支付担保
    /// 默认值: b'0'
    ///</summary>
    [Display(Name = "是否有工程款支付担保")]
    [Required(ErrorMessage = "{0}不能为空")]
    public bool HasEarnestMoney { get; set; }

    /// <summary>
    /// 担保单位名称
    ///</summary>
    [Display(Name = "担保单位名称")]
    public string? EarnestUnitName { get; set; }

    /// <summary>
    /// 担保单位统一社会信用代码
    ///</summary>
    [Display(Name = "担保单位统一社会信用代码")]
    public string? EarnestUnitCode { get; set; }

    /// <summary>
    /// 担保金额
    ///</summary>
    [Display(Name = "担保金额")]
    public decimal? EarnestMoney { get; set; }

    /// <summary>
    /// 担保开始日期
    ///</summary>
    [Display(Name = "担保开始日期")]
    public DateTime? EarnestStartDate { get; set; }

    /// <summary>
    /// 担保终止日期
    ///</summary>
    [Display(Name = "担保终止日期")]
    public DateTime? EarnestEndDate { get; set; }

    /// <summary>
    /// 担保文件附件列表
    /// </summary>
    [Display(Name = "担保文件附件列表")]
    public List<AttachmentsForm<int>>? EarnestAttachments { get; set; }

    /*扩展字段*/

    /// <summary>
    /// 项目名称
    /// </summary>
    public string ProjectName { get; set; } = string.Empty;

    /// <summary>
    /// 项目编号
    /// </summary>
    public string ProjectNum { get; set; } = string.Empty;

    /// <summary>
    /// 合同金额
    /// </summary>

    public decimal ContractAmount { get; set; }
}

/// <summary>
/// 扩展
/// </summary>

public static class ProjectCompletExtensions
{
    public static ProjectCompletForm AsCompletForm(this ProjectInfo projectInfo, IApplicationContext application)
    {
        ProjectCompletForm form = new();
        var mapper = application.GetService<IMapper>();
        mapper.Map(projectInfo, form);

        form.ProjectName = projectInfo.Name;
        form.ProjectNum = projectInfo.PrjNum;
        var projectAttachmentService = application.GetService<ProjectAttachmentService>();
        var attachments = projectAttachmentService.GetSysAttachmentByObjectIdAndType((int)form.ProjectId, 附件类型.担保文件, 项目附件租户.Project);
        form.EarnestAttachments = new List<AttachmentsForm<int>>();
        foreach (var item in attachments)
        {
            AttachmentsForm<int> attachmentsForm = new()
            {
                AttachmentId = item.Id,
                AttachmentName = item.FriendlyName,
            };
            form.EarnestAttachments.Add(attachmentsForm);
        }
        return form;
    }
}